package com.puhui.education.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.method.NumberKeyListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.puhui.education.R;
import com.zhy.android.percent.support.PercentLinearLayout;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/6/17.
 */
public class AccountInfoBar extends PercentLinearLayout {
    @Bind(R.id.tv_title_accountInfoBarCv)
    TextView tv_title;
    @Bind(R.id.et_content_accountInfoBarCv)
    EditText et_content;
    @Bind(R.id.tv_content_accountInfoBarCv)
    TextView tv_content;
    @Bind(R.id.tv_right_accountInfoBarCv)
    TextView tv_right;
    @Bind(R.id.pll_accountInfoBarCv)
    PercentLinearLayout pll;
    private Context context;

    public AccountInfoBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initAccountInfoBar();
        initAttrs(attrs);
    }

    private void initAccountInfoBar() {
        View view = LayoutInflater.from(context).inflate(R.layout.cv_account_info_bar, this);
        ButterKnife.bind(view);
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AccountInfoBar);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.AccountInfoBar_barType:
                    int barStyle = a.getInteger(R.styleable.AccountInfoBar_barType, 0);
                    if (barStyle == 1) {
                        tv_content.setVisibility(View.GONE);
                        tv_right.setVisibility(GONE);
                        et_content.setVisibility(View.VISIBLE);
                    }
                    break;
                case R.styleable.AccountInfoBar_tabText:
                    tv_title.setText(a.getString(R.styleable.AccountInfoBar_tabText));
                    break;
                case R.styleable.AccountInfoBar_hintText:
                    et_content.setHint(a.getString(R.styleable.AccountInfoBar_hintText));
                    break;
                case R.styleable.AccountInfoBar_editInputType:
                    String inputType = a.getString(R.styleable.AccountInfoBar_editInputType);
                    if (inputType.equals("Number")) {
                        et_content.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                    } else if (inputType.equals("Email")) {
                        et_content.setKeyListener(new NumberKeyListener() {
                            @Override
                            protected char[] getAcceptedChars() {
                                char[]numberChars=new char[64];
                                int i=0;
                                int n=48;
                                for ( ; i <10 ; i++) { //数字
                                    numberChars[i]= (char) n++;
                                }
                                int j=97;
                                for (; i < 36; i++) { //小写字母
                                    numberChars[i]=(char)j++;
                                }
                                int k=65;
                                for (; i < 62; i++) { //大写字母
                                    numberChars[i]=(char)k++;
                                }
                                numberChars[i++]='@';
                                numberChars[i]='.';
                                StringBuilder stringBuilder = new StringBuilder();
                                for (int m = 0; m <numberChars.length; m++) {
                                    stringBuilder.append(numberChars[m]);
                                }
//                                System.out.println(stringBuilder.toString());
                                return numberChars;
                            }

                            @Override
                            public int getInputType() {
                                return EditorInfo.TYPE_CLASS_TEXT;
                            }
                        });

                    }
                    break;
            }
        }
        a.recycle();
    }

    //设置bar点击事件
    public void setBarOnClickListener(OnClickListener listener) {
        pll.setOnClickListener(listener);
    }

    //设置选择文本
    public void setSelectText(String text) {
        tv_content.setText(text);
    }

    //获取选择文本
    public String getSelectText() {
        return tv_content.getText().toString();
    }

    //获取编辑文本
    public String getEditText() {
        return et_content.getText().toString();
    }
}
